import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-lesson8',
  templateUrl: './lesson8.component.html',
  styleUrls: ['./lesson8.component.scss']
})
export class Lesson8Component implements OnInit {

  list = [];
  startTime = null;
  endTime = null;
  timeLong = 0;
  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.getData();
  }
  /**
   * 获取数据
   */
  getData() {
    this.http.get('/api/getData').subscribe((res: any) => {
      if (res.errno === 0) {
        this.list = res.data;
      }
    });
  }

  /**
   * 发送爬取请求
   */
  sendPaQu() {
    this.startTime = new Date().getTime();
    this.http.post('/api/updateData', null).subscribe((res: any) => {
      if (res.errno === 0) {
        this.list = res.data;
      }
      this.endTime = new Date().getTime();
      this.timeLong = this.endTime - this.startTime;
    });
  }
}
